<?php

class InicioController extends CI_Controller {

    private $layout;

    public function __construct() {
        parent::__construct();
        $this->load->model("usuarioModel", "usuario");
        $this->layout['librerias'] = $this->load->view(TMPL_LIBRERIA, "", true);
        $this->layout['menu'] = $this->load->view(TMPL_MENU, "", true);
        $this->layout['pie'] = $this->load->view(TMPL_PIE, "", true);
    }

    public function index() {

        $this->form_validation->set_rules('email', 'EMAIL', 'required|valid_emails');
        $this->form_validation->set_rules('clave', 'CLAVE', 'required');

        if ($this->form_validation->run() == FALSE) {
            $this->layout['contenido'] = $this->load->view('login/index', "", true);
            $this->load->view(TMPL_GENERAL, $this->layout);
        } else {

            $email = $this->input->post('email');
            $clave = $this->input->post('clave');

            $respuesta = $this->usuario->login($email, $clave);

            if ($respuesta['estado'] == true) {
                $usuario = array(
                    'clave' => $respuesta['datos'][0]['vaclave'],
                    'email' => $respuesta['datos'][0]['vaemail'],
                    'dni' => $respuesta['datos'][0]['vadni'],
                    'idusuario' => $respuesta['datos'][0]['idusuario'],
                    'logged_in' =>TRUE
                );
                $this->session->set_userdata($usuario);
                redirect(site_url() . "/inicioController/menu");
            } else {
                redirect(site_url() . "/inicioController");
            }
        }
    }

    function menu() {
        $this->layout['contenido'] = $this->load->view('menu/index', "", TRUE);
        $this->load->view(TMPL_GENERAL, $this->layout);
    }

}

?>
